<html>
<head>
<title>Memory Game</title>
<link rel="stylesheet" type="text/css" href="fil.css" />

<script type="text/javascript">
var words=["Saab","Volvo","BMW","VW","Toyota","GM","Honda","Ford"];
var times=[0,0,0,0,0,0,0,0];
var size = 0;

var turned1 = null;
var turned2 = null;

var players = [0,0];
var activeplayer = 0;
var numberofturned = 0;

function tdClick(el)
{
if(turned2!=null){
   if(turned1.innerHTML != turned2.innerHTML){
      turned1.style.backgroundColor="black";
      turned2.style.backgroundColor="black";
      if(activeplayer==0){
         activeplayer=1;
      } else {
	 activeplayer=0;
      }
      document.getElementById("score").rows[0].cells[1].innerHTML = activeplayer+1;
   }
   turned1=null;
   turned2=null;
} else {
if(el==turned1 || el.style.backgroundColor=="white"){
   return;
}
el.style.backgroundColor="white";
if(turned1==null){
   turned1 = el;
} else {
   turned2=el;
   if(turned1.innerHTML == turned2.innerHTML){
      turned1=null;
      turned2=null;
      numberofturned = numberofturned+1;
      players[activeplayer]++;
      document.getElementById("score").rows[activeplayer+1].cells[1].innerHTML = players[activeplayer];
      if(numberofturned==size){
         var winningplayer = 0;
	 if(players[0]>players[1]){
	    alert("Player 1 wins");
         } else {
            if(players[0]<players[1]){
	       alert("Player 2 wins");
            } else {
               alert("Tie");
            }
         }
         document.location = document.location;
      }
   }
}
}
}

function load()
{
size = words.length;
table = document.getElementById("board");
for (var i = 0, row; row = table.rows[i]; i++) {
   //iterate through rows
   //rows would be accessed using the "row" variable assigned in the for loop
   for (var j = 0, col; col = row.cells[j]; j++) {
     var rn=Math.floor(Math.random()*words.length)
     col.innerHTML = words[rn];
     times[rn]=times[rn]+1;
     if(times[rn]==2){
	words.splice(rn,1);
	times.splice(rn,1);
     }
     //iterate through columns
     //columns would be accessed using the "col" variable assigned in the for loop
   }  
}
}
</script>






</head>
<body onload="load()">
<table class="board" id="board" border="2">
<tr>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
</tr>
<tr>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
</tr>
<tr>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
</tr>
<tr>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
<td onclick="tdClick(this)" >jkjk</td>
</tr>
</table>
<table id="score" border="0">
<tr>
<td >Activeplayer:</td>
<td >1</td>
</tr>
<tr>
<td >Player1:</td>
<td >0</td>
</tr>
<tr>
<td >Player2:</td>
<td >0</td>
</tr>
</table>
</body></html>

